﻿Imports SACommon

Public Class ucSettings
    Private mAppSettings As AppSettings
    Private mAppSettingsID As Integer
    Private mAppSettingsItem As AppSettingsItem
    Private Sub ucSettings_Load(sender As Object, e As EventArgs) Handles Me.Load
        lblSettingsHeader.Text = "Settings: " & gUDApp.SelectedApp.ApplicationTitle
        mAppSettings = New AppSettings
        mAppSettingsItem = New AppSettingsItem
        mAppSettingsID = 0

        lvwSettings.Columns(0).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
        lvwSettings.Columns(1).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)
        lvwSettings.Columns(2).AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent)

        Extensions = New Dictionary(Of [String], [String])()
        Extensions.Add("yes", "Active")
        Extensions.Add("no", "Deleted")

        For Each kvp As KeyValuePair(Of [String], [String]) In Extensions
            lvwSettings.Groups.Add(kvp.Key, kvp.Value)
        Next

        LoadSettings()
        ResetFields()
    End Sub

#Region "Load Settings"
    Sub LoadSettings()
        Dim tMatch As Boolean
        UseWaitCursor = True
        lvwSettings.Items.Clear()

        lvwSettings.BeginUpdate()

        Dim tString As String

        For Each item In mAppSettings.SettingsList
            tMatch = True

            Dim lvi As ListViewItem = lvwSettings.Items.Add(item.ID)

            lvi.SubItems.Add(item.Description)
            lvi.SubItems.Add(item.Value)

            If item.StatusID = SAEventArgs.StatusCodes.Retired Then
                tString = "no"
            Else
                tString = "yes"
                lvi.Checked = True
            End If

            lvi.Tag = item.SettingsID
            lvi.Group = lvwSettings.Groups(tString)
        Next

        lvwSettings.Columns(0).Width = -1
        lvwSettings.Columns(1).Width = -1
        lvwSettings.Columns(2).Width = -2

        lvwSettings.EndUpdate()
        UseWaitCursor = False
    End Sub
#End Region
#Region "Properties"
    Public Property Extensions() As Dictionary(Of [String], [String])
        Get
            Return m_Extensions
        End Get
        Private Set(value As Dictionary(Of [String], [String]))
            m_Extensions = value
        End Set
    End Property
    Private m_Extensions As Dictionary(Of [String], [String])
#End Region
#Region "Process UI"
    Private Sub lvwSettings_Click(sender As Object, e As EventArgs) Handles lvwSettings.Click
        If lvwSettings.SelectedItems.Count > 0 Then
            mAppSettingsID = CInt(lvwSettings.SelectedItems(0).Tag)
            mAppSettingsItem = mAppSettings.GetItem(mAppSettingsID)
            txtDescription.Text = mAppSettingsItem.Description.Trim
            txtValue.Text = mAppSettingsItem.Value.Trim
            txtID.Text = mAppSettingsItem.ID
            btnAdd.Text = "Update"
        Else
            mAppSettingsID = 0
        End If
    End Sub
    Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
        ResetFields()
    End Sub
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim tID As Integer
        Dim tString As String = ""
        If txtDescription.Text = "" Then
            Messenger.SendMessage(Messenger.MessageType.GeneralMessage, "Please enter a description", True)
            Exit Sub
        End If

        Try
            tString = "Please enter an Integer as the ID"
            tID = CInt(txtID.Text)
            tString = String.Format("Please enter a valid ID.")
        Catch ex As Exception
            Messenger.SendMessage(Messenger.MessageType.GeneralMessage, tString, True)
            Exit Sub
        End Try

        mAppSettingsItem.ID = tID
        mAppSettingsItem.Description = txtDescription.Text
        mAppSettingsItem.Value = txtValue.Text
        mAppSettings.Save(mAppSettingsItem)

        LoadSettings()
        ResetFields()
    End Sub
#End Region
#Region "General Functions"
    Sub ResetFields()
        mAppSettingsID = 0
        btnAdd.Text = "Add"
        txtDescription.Text = ""
        txtValue.Text = ""
        txtID.Text = mAppSettings.GetNextID
    End Sub
#End Region
End Class
